layui.config({
    base: CXL.ctxPath + '/layuiadmin/' //静态资源所在路径
}).extend({
    index: 'lib/index' //主入口模块
}).use(['index', 'table', 'laydate'], function () {
    var $ = layui.$;
    let updateStartUpStatus = function (traName, startUp) {
        let className = "." + traName + "StatusBtn-start-up";
        if (startUp) {
            $(className).addClass("layui-btn-danger");
            $(className).text("关闭");
        } else {
            $(className).removeClass("layui-btn-danger");
            $(className).text("启动");
        }
    }
    let updateWorkStatus = function (traName, traStatus) {
        let className = "." + traName + "StatusBtn-start";
        if (traStatus == "工作中") {
            $(className).addClass("layui-btn-disabled");
            $(className).text("工作中");
            $(className).removeClass("layui-btn-disabled");
            $(className).text("停止任务");

        } else {
            // console.log(traName+" "+traStatus);
            $(className).removeClass("layui-btn-disabled");
            $(className).text("开始");
            $(className).addClass("layui-btn-disabled");
            $(className).text("暂无任务");
        }
    }
    let updateWorkInfo = function (traName, workInfo) {
        $("." + traName + "-work-type").text(workInfo.type);
        $("." + traName + "-work-mix-max").text(workInfo.minX + " ~ " + workInfo.maxX);
        $("." + traName + "-work-start-position").text(workInfo.grabPointer);
        $("." + traName + "-work-end-position").text(workInfo.unwindPointer);
        $("." + traName + "-work-material").text(workInfo.materialName);
    }
    let getWorkStatus = function () {
        $.ajax({
            url: CXL.ctxPath + '/work/getWorkStatus',
            method: 'GET',
            datatype: 'JSON',
            success: function (res) {
                if (res.success) {
                    $(".firstTraStatus").text(res.obj.firstTraStatus);
                    $(".secondTraStatus").text(res.obj.secondTraStatus);
                    $(".thirdTraStatus").text(res.obj.thirdTraStatus);
                    $(".fourthTraStatus").text(res.obj.fourthTraStatus);
                    //更新天车启动信息
                    updateStartUpStatus("firstTra", res.obj.workStatus.firstTraStartUp);
                    updateStartUpStatus("second", res.obj.workStatus.secondTraStartUp);
                    updateStartUpStatus("third", res.obj.workStatus.thirdTraStartUp);
                    updateStartUpStatus("fourth", res.obj.workStatus.fourthTraStartUp);

                    //更新任务明细信息
                    updateWorkInfo("first", res.obj.firstTraInfo);
                    updateWorkInfo("second", res.obj.secondTraInfo);
                    updateWorkInfo("third", res.obj.thirdTraInfo);
                    updateWorkInfo("fourth", res.obj.fourthTraInfo);
                }
            }
        });
    }
    let startUp = function (tra) {
        let loadIndex = CXL.load();
        $.ajax({
            url: CXL.ctxPath + '/work/startUp',
            method: 'GET',
            datatype: 'JSON',
            data: { tra: tra },
            success: function (res) {
                CXL.closeByIndex(loadIndex, null);
                if (res.success) {
                    CXL.success("天车启动成功");
                } else {
                    CXL.error(res.msg);
                }
            },
            fail: function () {
                CXL.closeByIndex(loadIndex, null);
            }
        });
    }

    let addWait = function (tra) {
        let loadIndex = CXL.load();
        $.ajax({
            url: CXL.ctxPath + '/work/addTraWait',
            method: 'GET',
            datatype: 'JSON',
            data: { tra: tra },
            success: function (res) {
                CXL.closeByIndex(loadIndex, null);
                if (res.success) {
                    CXL.success("天车初始化成功");
                } else {
                    CXL.error(res.msg);
                }
            },
            fail: function () {
                CXL.closeByIndex(loadIndex, null);
            }
        });
    }

    let closeUp = function (tra) {
        let loadIndex = CXL.load();
        $.ajax({
            url: CXL.ctxPath + '/work/closeUp',
            method: 'GET',
            datatype: 'JSON',
            data: { tra: tra },
            success: function (res) {
                CXL.closeByIndex(loadIndex, null);
                if (res.success) {
                    CXL.success("天车关闭成功");
                } else {
                    CXL.error(res.msg);
                }
            },
            fail: function () {
                CXL.closeByIndex(loadIndex, null);
            }
        });
    }

    $(".add-wait").click(function () {
        let tra = $(this).attr('tra');
        addWait(tra);

    });
    $(".start-up").click(function () {
        let tra = $(this).attr('tra');
        if ($(this).hasClass('layui-btn-danger')) {
            closeUp(tra);
        } else {
            startUp(tra);
        }

    });

    setInterval(function () {
        getWorkStatus();
    }, 1000);
    $(".start").click(function () {
        if ($(this).hasClass('layui-btn-disabled')) {
            return;
        }
        let loadIndex = CXL.load();
        let tra = $(this).attr('tra');
        $.ajax({
            url: CXL.ctxPath + '/work/notifyTra',
            method: 'GET',
            datatype: 'JSON',
            data: { tra: tra },
            success: function (res) {
                CXL.closeByIndex(loadIndex, null);
                if (res.success) {
                    CXL.success("开始进行获取任务，任务生成后会自动执行。");
                } else {
                    CXL.error(res.msg);
                }
            },
            fail: function () {
                CXL.closeByIndex(loadIndex, null);
            }
        });
    });
    $(".stop").click(function () {
        if ($(this).hasClass('layui-btn-disabled')) {
            return;
        }
        let tra = $(this).attr('tra');
        CXL.confirm("确定停止该天车的任务吗？", function () {
            let loadIndex = CXL.load();
            $.ajax({
                url: CXL.ctxPath + '/work/stopWorkByTra',
                method: 'GET',
                datatype: 'JSON',
                data: { tra: tra },
                success: function (res) {
                    CXL.closeByIndex(loadIndex, null);
                    if (res.success) {
                        CXL.success("任务停止成功。");
                    } else {
                        CXL.error(res.msg);
                    }
                },
                fail: function () {
                    CXL.closeByIndex(loadIndex, null);
                }
            });
        });
    });
});